package com.gitee.huxing.algorithmic.stack;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @Description:
 * @ProjectName: AlgorithmicPactice
 * @Package: com.gitee.huxing.algorithmic.stack
 * @ClassName: Solution
 * @Author: huxing
 * @DateTime: 2021-12-16 5:08 下午
 */
public class Solution {

    public boolean isValid(String s){
        // 先判断字符串长度是否为偶数，不为偶数的话直接抛弃
        int n = s.length();
        if (n % 2 == 1){
            return false;
        }

        // 创建一个匹配视图
        Map<Character, Character> pairs = new HashMap<Character, Character>() {{
            put(')', '(');
            put(']', '[');
            put('}', '{');
        }};

        // 创建一个栈
        Deque<Character> stack = new LinkedList<Character>();

        for (int i = 0; i < n; i++){
            char ch = s.charAt(i);
            if (pairs.containsKey(ch)){
                if (stack.isEmpty() || !stack.peek().equals(pairs.get(ch))){
                    return false;
                }
                stack.pop();
            } else {
                stack.push(ch);
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        int num = 0 ;
        for (int counter = 0; counter <= 170; counter++){
            num += counter * 15;
        }
        System.out.println("满170级以后的亲密: " + num);
//        Solution solution = new Solution();
//        System.out.println(solution.isValid("{(({[]})[]{})}"));
//        System.out.println(solution.isValid("{[()]}"));
//        int[] nums = new int[]{1,2,3,1,2,3};
//        System.out.println(containsNearbyDuplicate(nums, 2));
//        System.out.println(isPalindrome(121));
        int[] nums = new int[]{-2,1,-3,4,-1,2,1,-5,4};
        System.out.println(maxSubArray(nums));
    }

    public static boolean containsNearbyDuplicate(int[] nums, int k) {
        int len = nums.length;
        if (len == 0){
            return false;
        }
        for (int i = 0; i < len; i++){
            for (int j= i + 1; j<len; j++){
                if (nums[i] == nums[j] && (j-i) <= k){
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isPalindrome(int x) {
        // 特殊情况：
        // 如上所述，当 x < 0 时，x 不是回文数。
        // 同样地，如果数字的最后一位是 0，为了使该数字为回文，
        // 则其第一位数字也应该是 0
        // 只有 0 满足这一属性
        if (x < 0 || (x % 10 == 0 && x != 0)){
            return false;
        }
        int revertedNumber = 0;
        while (x > revertedNumber){
            revertedNumber = revertedNumber * 10 + x % 10;
            x /= 10;
        }
        return x == revertedNumber || x == revertedNumber / 10;
    }

    public static int maxSubArray(int[] nums) {
        int pre = 0, maxAns = nums[0];
        for (int x : nums) {
            pre = Math.max(pre + x, x);
            maxAns = Math.max(maxAns, pre);
        }
        return maxAns;
    }
}
